﻿/*****************************************************************
 *Yuga Technology
 *@author YUga studio
 *@copyright all rights reserved (c) zengkang007@gmail.com
 *****************************************************************/
package com.oplink.webbase.modules.invoicing.web;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.oplink.webbase.common.persistence.Page;
import com.oplink.webbase.modules.invoicing.entity.Supplier;
import com.oplink.webbase.modules.invoicing.service.SupplierService;

import java.util.Map;

import javax.servlet.ServletRequest;
import javax.validation.Valid;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
import org.springside.modules.web.Servlets;

import com.oplink.webbase.modules.sys.entity.User;
import com.oplink.webbase.modules.sys.service.ShiroDbRealm.ShiroUser;
import com.oplink.webbase.modules.sys.web.BaseController;
import com.oplink.webbase.modules.invoicing.entity.Supplier;
import com.oplink.webbase.modules.invoicing.service.SupplierService;

/**
 * Supplier管理的Controller, 使用Restful风格的Urls:
 * 
 * List page : GET /queue/
 * Create pushlet : GET /queue/pushlet
 * Create page : GET /queue/create
 * Create action : POST /queue/create
 * Update page : GET /queue/update/{id}
 * Update action : POST /queue/update
 * Delete action : GET /queue/delete/{id}
 * 告警Controller
 * @author YUga studio
 * @version 2015-01-12
 */
@Controller
@RequestMapping(value = "invoicing/supplier")
public class SupplierController extends BaseController {
	
	@Autowired
	private SupplierService supplierService;
	
	@RequiresPermissions("modules/invoicing:supplier:view")
	@RequestMapping(value = {"list", ""}, method = {RequestMethod.GET, RequestMethod.POST})
	public String list(Supplier supplier, HttpServletRequest request, HttpServletResponse response, Model model) {
		
		Map<String, Object> searchParams = Servlets.getParametersStartingWith(request, "search_");
		Long userId = getCurrentUserId();

		Page<Supplier> pages = supplierService.find(new Page<Supplier>(request, response), supplier);         
		//Page<Supplier> pages = supplierService.getUserSupplier(userId, searchParams, pageNumber, pageSize, sortType);
		model.addAttribute("pages", pages);
		model.addAttribute("userId", userId);

		// 将搜索条件编码成字符串，用于排序，分页的URL
		model.addAttribute("searchParams", Servlets.encodeParameterStringWithPrefix(searchParams, "search_"));
		return "modules/invoicing/supplierList";
	}

	@RequiresPermissions("modules/invoicing:supplier:edit")
	@RequestMapping(value = "update/{id}", method = RequestMethod.GET)
	@ResponseBody
	public Supplier updateForm(@PathVariable("id") Long id) {
		Supplier supplier = supplierService.getSupplier(id);
		supplier.setUser(null);
		return supplier;
	}
	
	@RequestMapping(value = "update", method = RequestMethod.POST)
	@ResponseBody
	public String update(@Valid @ModelAttribute("supplier") Supplier supplier, RedirectAttributes redirectAttributes) {
		try {
			User user = new User(getCurrentUserId());
			supplier.setUser(user);
			supplierService.saveSupplier(supplier);
			redirectAttributes.addFlashAttribute("message", "更新任务成功");
			return "1";
		} catch (Exception e) {
			return "0";
		}
	}
	
	@RequiresPermissions("modules/invoicing:supplier:view")
	@RequestMapping(value = "form")
	public String form(Supplier supplier, Model model) {
		model.addAttribute("supplier", supplier);
		return "modules/invoicing/supplierForm";
	}

	@RequiresPermissions("modules/invoicing:supplier:edit")
	@RequestMapping(value = "create", method = RequestMethod.GET)
	public String createForm(Model model) {
		model.addAttribute("userId", getCurrentUserId());
		model.addAttribute("action", "create");
		return "modules/invoicing/supplier/create";
	}

	@RequestMapping(value = "create", method = RequestMethod.POST)
	@ResponseBody
	public String create(Supplier supplier, RedirectAttributes redirectAttributes, Model model) {
		try {
			User user = new User(getCurrentUserId());
			supplier.setUser(user);
			model.addAttribute("supplier", supplier);
			supplierService.saveSupplier(supplier);
			redirectAttributes.addFlashAttribute("message", "创建任务成功");
			return "1";
		} catch (Exception e) {
			e.printStackTrace();
			return "0";
		}
	}

	@RequestMapping(value = "delete/{id}")
	@ResponseBody
	public String delete(@PathVariable("id") String id, RedirectAttributes redirectAttributes) {
		try {
			String ret = "0";
			if (!"".equals(id)) {
				String[] delList = id.split(",");
				for (String idItem : delList) {
					supplierService.deleteSupplier(Long.valueOf(idItem));
				}
				redirectAttributes.addFlashAttribute("message", "删除任务成功");
				ret = "1";
			} else {
				ret = "0";
			}
			return ret;
		} catch (Exception e) {
			return "0";
		}
	}
	
	/**
	 * 取出Shiro中的当前用户Id.
	 */
	private Long getCurrentUserId() {
		ShiroUser user = (ShiroUser) SecurityUtils.getSubject().getPrincipal();
		return user.id;
	}
}
